<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .account-funding-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .img{
   width: 150px;
   height: 100px;
   float: left;
   margin: 5px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>商户账户列表</span>
    </div>
    <div class="line"></div>
    <account-funding-form
      class="account-funding-form"
      @on-success-valid="handleSubmit">
    </account-funding-form>
    <account-funding-drawer
      :modalState="modalState"
      :row="row"
      @on-cancel="cancel"
      @account-funding-valid="handleEdit">
    </account-funding-drawer>
    <funding-confirm-modal
      :modalState="confirmState"
      @cancel-confirm="cancelConfirm"
      @confirm-valid="handleConfirm"></funding-confirm-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import AccountFundingForm from '_c/account-funding-form'
import AccountFundingDrawer from '_c/account-funding-drawer'
import FundingConfirmModal from '_c/funding-confirm-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    AccountFundingForm,
    AccountFundingDrawer,
    FundingConfirmModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '财务管理商户账户列表',
      index: '',
      confirmData: {},
      modalState: false,
      confirmState: false,
      loading: true,
      playload: null,
      drawer: null,
      rows: 20,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '商户姓名',
          key: 'uname',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.uname ? params.row.uname : '-')
          }
        },
        // {
        //   title: '内部销售',
        //   key: 'salename',
        //   width: 200,
        //   align: 'center'
        // },
        {
          title: '商户电话',
          key: 'tel',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.tel ? params.row.tel : '-')
          }
        },
        {
          title: '充值总金额',
          key: 'tprice',
          minWidth: 130,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.tprice ? params.row.tprice : '0')
          }
        },
        {
          title: '消费总金额',
          key: 'alluse',
          minWidth: 130,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.alluse ? params.row.alluse : '0')
          }
        },
        {
          title: '佣金余额',
          key: 'commission',
          minWidth: 130,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.commission ? params.row.commission : '-')
          }
        },
        {
          title: '服务费余额',
          key: 'service',
          minWidth: 130,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.service ? params.row.service : '-')
          }
        },
        {
          title: '注册日期',
          key: 'ctime',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.ctime ? params.row.ctime : '-')
          }
        },
        // {
        //   title: '更新时间',
        //   key: 'updatetime',
        //   align: 'center',
        //   width: 180
        // },
        {
          title: '操作',
          key: 'cz',
          minWidth: 196,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row)
                  }
                }
              }, '充值')
              // h('Button', {
              //   props: {
              //     type: 'error',
              //     size: 'small',
              //     ghost: 'ghost'
              //   },
              //   style: {
              //     marginRight: '5px'
              //   },
              //   on: {
              //     click: () => {
              //       this.handleDelete(params.index)
              //     }
              //   }
              // }, '删除')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      fundingList: state => state.funding.fundingList,
      dataCount: state => state.funding.fundingDataCount,
      historyData: state => state.funding.fundingHistoryData,
      pageSize: state => state.funding.fundingPageSize,
      row: state => state.funding.row
    })
  },
  methods: {
    ...mapActions([
      'getFundingList',
      'getFundingCurrentRow',
      'editFundingData',
      'changeFundingPage',
      'changeFundingPageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      // console.log(this.playload)
      this.getFundingList(playload).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (row) {
      // console.log(row)
      let newRow = {}
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getFundingCurrentRow(newRow)
      this.modalState = true
    },
    async handleEdit (playload) {
      this.drawer = playload.drawer
      this.confirmData = {
        tid: playload.tid,
        tel: playload.tel,
        type: playload.type,
        paytype: playload.paytype,
        price: playload.price,
        des: playload.des
      }
      // console.log(playload)
      this.modalState = false
      this.confirmState = true
    },
    async handleConfirm () {
      this.loading = true
      const resp = await this.editFundingData(this.confirmData)
      if (resp.data.state === '000') {
        this.$Message.success('充值成功')
        this.getFundingList({
          page: this.page,
          rows: this.rows,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.confirmState = false
      this.drawer.resetFields()
      // this.updateConfirmModalState(true)
    },
    cancel (value) {
      this.modalState = false
      value.resetFields()
    },
    cancelConfirm () {
      this.confirmState = false
      this.modalState = true
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeFundingPage(index)
      this.getFundingList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeFundingPageSize(value)
      this.changePage(this.page)
      this.getFundingList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getFundingList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
